﻿using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dispatcher;

namespace OwinSelfHostWithWebApi
{
    /// <summary>
    /// OWIN会自动调用Startup类，的Configuration方法
    /// </summary>
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            
            // 让WebApi支持Namespace
            config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));

            config.Formatters.Clear();
            System.Net.Http.Formatting.JsonMediaTypeFormatter formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();

            // 设置日期格式
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };
            formatter.SerializerSettings.Converters.Add(dtConverter);
            // 采用驼峰式命名
            formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            config.Formatters.Add(formatter);
            /*
            #region 使用这种映射模式，顺序上有一定的要求
            // 带模块定义的映射必须放在最上面
            config.Routes.MapHttpRoute(
                name: "DefaultApi-v3",
                routeTemplate: "api/v3/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new string[] { "OwinSelfHostWithWebApi.V3" }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApi-v2",
                routeTemplate: "api/v2/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new string[] { "OwinSelfHostWithWebApi.V2" }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new string[] { "OwinSelfHostWithWebApi" }
            );
            #endregion
             * */
            #region 使用这种映射模式，顺序上有一定的要求
            // 带模块定义的映射必须放在最上面
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new string[] { "OwinSelfHostWithWebApi" }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApi-v3",
                routeTemplate: "api-v3/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new string[] { "OwinSelfHostWithWebApi.V3" }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApi-v2",
                routeTemplate: "api-v2/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new string[] { "OwinSelfHostWithWebApi.V2" }
            );
            #endregion

            appBuilder.UseWebApi(config);
        }
    }
}
